<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
 * PHP version 5
 *
 * This source file is subject to version 2.1 of the GNU Lesser General Public
 * License, that is bundled with this package in the file COPYING, available
 * through the world wide web at the following URI:
 * http://www.gnu.org/copyleft/lesser.html.
 *
 * @package    ZNF
 * @subpackage Presentation
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: XSLT.php 43 2007-06-26 23:36:35Z aronnax $
 * @since      Release 0.5.0
 * @link       http://www.zeronotice.org
 */

require_once('ZNF/Presentation/Render.php');

/**
 * <i>ZNF_Presentation_XSLT</i> class, extends the <i>ZNF_Presentation_Render</i> class and implements renderization with PHP <i>XSLTProcessor</i>.
 *
 * Render XML data using package or theme XSLT.
 *
 * @access     public
 * @package    ZNF
 * @subpackage Presentation
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: XSLT.php 43 2007-06-26 23:36:35Z aronnax $
 * @since      Release 0.5.0
 * @link       http://www.zeronotice.org
 */
class ZNF_Presentation_XSLT extends ZNF_Presentation_Render
{

    /**
     * The <i>XSLTProcessor</i>.
     *
     * @access protected
     * @var XSLTProcessor $_xslt
     */
     protected $_xslt;

    /**
     * Where assigned XSLT parameters are kept.
     *
     * @access protected
     * @var array $_xsltParams
     */
     protected $_xsltParams = array();

    /**
     * Constructs a new <i>ZNF_Presentation_XSLT</i> object and sets the internal properties with application settings.
     *
     * @access public
     * @param string $package Name of the package to which the xslt template
     *                        refers, leave null this parameter to refer to the
     *                        theme xslt template
     * @param ZNF_Action_ActionMapping $mapping
     */
    public function __construct($package = null, $mapping = null)
    {
        parent::__construct($package, $mapping);

        $this->_xslt = new XSLTProcessor();
    }

    /**
     * Executes and returns the xslt template results.
     *
     * @access public
     * @param string $template File name of the xslt template
     * @param string $xml The XML document
     * @return string
     */
    public function fetch($template, $xml = null)
    {
        $template = $this->_getTemplatePath($template);

        $this->_xslt->importStylesheet(DOMDocument::load($template));
        $this->_assignConstants($template);

        // change this istruction when the xsltprocessor->setParameter() is fixed to accept an array
        foreach ($this->_xsltParams as $name => $value) {
            $this->_xslt->setParameter('znf', $name, $value);
        }

        return $this->_xslt->transformToXml(DOMDocument::loadXML($xml));
    }

    /**
     * Executes and displays the xslt template results.
     *
     * @access public
     * @param string $template File name of the xslt template
     * @param string $xml The XML document
     */
    public function display($template, $xml = null)
    {
        $template = $this->_getTemplatePath($template);

        $this->_xslt->importStylesheet(DOMDocument::load($template));
        $this->_assignConstants($template);

        // change this istruction when the xsltprocessor->setParameter() is fixed to accept an array
        foreach ($this->_xsltParams as $name => $value) {
            $this->_xslt->setParameter('znf', $name, $value);
        }

        echo $this->_xslt->transformToXml(DOMDocument::loadXML($xml));
    }

    /**
     * Assigns values to xslt template parameters.
     *
     * @access public
     * @param string $param The xslt template parameter name
     * @param mixed $value The value to assign
     */
    public function assign($param, $value)
    {
        if ($param) {
            $this->_xsltParams[$param] = $value;
        }
    }

    /**
     * Assign to xslt template the ZNF constants.
     *
     * @access protected
     */
    protected function _assignConstants()
    {
        $constants = $this->_getConstants();

        $this->assign('znf.baseHref', $constants['baseHref']);
        $this->assign('znf.actionIndex', $constants['actionIndex']);
        $this->assign('znf.moduleIndex', $constants['moduleIndex']);
        $this->assign('znf.themePath', $constants['themePath']);
        $this->assign('znf.packagePath', $constants['packagePath']);
        $this->assign('znf.path', $constants['path']);
        $this->assign('znf.nextPath', $constants['nextPath']);
        $this->assign('znf.credits', $constants['credits']);
    }

    /**
     * Destroys the <i>ZNF_Presentation_XSLT</i> object.</>
     *
     * @access public
     */
    public function __destruct()
    {
    }

}

/**
 * <i>ZNF_Presentation_XSLTException</i> is the exception type for the <i>ZNF_Presentation_XSLT</i> class.
 *
 * <i>ZNF_Presentation_XSLTException</i> extends the <i>Exception</i> class of PHP5.
 *
 * @access     public
 * @package    ZNF
 * @subpackage Presentation
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: XSLT.php 43 2007-06-26 23:36:35Z aronnax $
 * @since      Release 0.5.0
 * @link       http://www.zeronotice.org
 */
class ZNF_Presentation_XSLTException extends Exception
{
}

?>
